import pandas as pd
import re
import numpy as np
from keras.utils import np_utils

ic_file = '../data/xulian.csv'


def dealWith_brand_alias(dataArray):
    i = 0
    for item in dataArray:
        i += 1
        ## 判断数据类型
        if type(item) is str:
            ## 去掉两端的"[" 和 "]"
            item = item[1:-1]
            ##  按照逗号切分，返回数组
            dataArr = item.split(",")
            dataArray[i] = dataArr
        else:
            dataArray[i] = []
    return dataArray

def usefull_filed(ic_file):
    output = open("../data/douban.txt", 'w')
    data = pd.read_csv(ic_file)
    brand_alias = data["brand_alias"]
    array = list(dealWith_brand_alias(brand_alias[1:1000]))
    n = 1
    for itemArr in array:
        strData = str(data['part_number'][1:1000][n]) + "," + str(data['brand'][1:1000][n]) + "," + str(data['part_desc'][1:1000][n]) + "," + ','.join(itemArr)
        output.write(strData)
        output.write("\n")
        n += 1
    output.close()

def strconversionint(strdata,char_to_n):
    X = []
    i = 0
    for str in strdata.split("\n"):
        i += 1
        if i < len(strdata.split("\n")):
            dataArr = re.split(',| |\n', str)
            dataArr.insert(0, ' ')
            X.append([char_to_n[strInde] for strInde in dataArr])
    for item in X:
        while len(item) < 80:
            item.append(0)
        item.reverse()
    X_modified = np.reshape(X, (len(X), 80, 1))
    Y = []
    for n in X_modified:
        Y.append(n[80-2][0])
    Y = np_utils.to_categorical(np.array(Y))
    print(Y)
    # X_modified = np.delete(X_modified, 1, 1)
    X_modified = X_modified / float(len(char_to_n))
    return (X_modified,Y)

def teststrconversionint(strdata,char_to_n,n_to_char):
    X = []
    strdata = strdata.lower()
    dataArr = re.split(',| |\n', strdata)
    X.append([char_to_n[strInde] for strInde in dataArr])
    for item in X:
        while len(item) < 80:
            item.append(0)
        item.reverse()
    X_modified = np.reshape(X, (len(X), 80, 1))
    # X_modified = np.delete(X_modified, 1, 1)
    X_modified = X_modified / float(len(n_to_char))
    return X_modified


def readdata():
    text = (open("../data/douban.txt").read())
    ## 将大写转换为小写
    text = text.lower()
    # re模块多种情况切割
    strArr = re.split(',| |\n',text)
    # 切割后的数组首位添加空格
    strArr.insert(0, ' ')
    # 字符映射成数字
    char_to_n = {char: n for n, char in enumerate(strArr)}
    n_to_char = {n: char for n, char in enumerate(strArr)}
    (X_modified,Y) = strconversionint(text,char_to_n)
    ## X_modified的值进行缩放，这样我们的神经网络就可以更快地训练
    # X_modified = X_modified / float(len(strArr))
    # print(X_modified)
    return (X_modified,n_to_char,char_to_n,Y)

# usefull_filed(ic_file)
# readdata()